package com.yam.michael.hibernate.dao.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.xunjia.exception.XunjiaException;
import com.yam.michael.hibernate.dao.HyperlinkAdvInfoDao;
import com.yam.michael.hibernate.pojo.HyperlinkAdvInfo;
public class HyperlinkAdvInfoDaoImpl extends HibernateDaoSupport implements HyperlinkAdvInfoDao,Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2167336517924941073L;
	//日志
	private static final Logger logger =Logger.getLogger(HyperlinkAdvInfoDaoImpl.class);
	@Override
	/**
	 * @category 添加一条超链接广告
	 * @param hyperlinkAdvInfo
	 * @return
	 * @throws Exception
	 */
	public void addHyperlinkAdvInfo(HyperlinkAdvInfo hyperlinkAdvInfo)
			throws XunjiaException {
		try {
			this.getHibernateTemplate().saveOrUpdate(hyperlinkAdvInfo);
		} catch (DataAccessException e) {
			logger.info(e.getMessage());
			throw new XunjiaException("添加超链接广告错误!!!");
		}
	}
	@Override
	/**
	 * @category 通过ID获得超链接广告信息
	 * @param sid  超链接广告ID
	 * @return
	 * @throws XunjiaException
	 */
	public HyperlinkAdvInfo getHyperlinkAdvInfoById(long hid) throws XunjiaException {
		try {
			return (HyperlinkAdvInfo) this.getHibernateTemplate().get(HyperlinkAdvInfo.class, hid);
		} catch (DataAccessException e) {
			logger.info(e.getMessage());
			throw new XunjiaException("通过ID获得超链接广告信息发生错误!!!");
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	/**
	 * @category 获取广告拥有者所有超链接广告列表
	 * @param owner_userId  广告拥有者ID
	 * @return
	 * @throws XunjiaException
	 */
	public List<HyperlinkAdvInfo> getHyperlinkAdvInfoList(final String ownerUserId,final int rowNum,final int pageNum)
			throws XunjiaException {
			try {
				return (List<HyperlinkAdvInfo>) this.getHibernateTemplate().execute(new HibernateCallback(){
				@Override
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query q = session.createQuery("FROM HyperlinkAdvInfo as h WHERE h.ownerUserId=?");
					q.setString(0, ownerUserId);
					q.setFirstResult(rowNum);
					q.setMaxResults(pageNum);
					return q.list();
				}});
			} catch (DataAccessException e) {
				logger.info(e.getMessage());
				throw new XunjiaException("通过用户超链接广告 列表发生错误,获取失败!!!");
			}
	}

	@Override
	/**
	 * @category  更新一条超链接广告信息
	 * @param hyperlinkAdvInfo
	 * @return
	 * @throws XunjiaException
	 */
	public void updateHyperlinkAdvInfo(HyperlinkAdvInfo hyperlinkAdvInfo)
			throws XunjiaException {
		try {
			this.getHibernateTemplate().saveOrUpdate(hyperlinkAdvInfo);
		} catch (DataAccessException e) {
			logger.info(e.getMessage());
			throw new XunjiaException("更新超链接广告发生错误,更新失败!!!");
		}
	}

	@Override
	/**
	 * @category 删除用户/企业超链接广告
	 * @param hyperlinkAdvInfo
	 * @return
	 * @throws XunjiaException
	 */
	public void deleteHyperlinkAdvInfo(HyperlinkAdvInfo hyperlinkAdvInfo)
			throws XunjiaException {
		try {
			this.getHibernateTemplate().delete(hyperlinkAdvInfo);
		} catch (DataAccessException e) {
			logger.info(e.getMessage());
			throw new XunjiaException("删除超链接广告发生错误,删除失败!!!");
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	/**
	 * 通过广告位置获取超链接广告
	 * @param position
	 * @return
	 * @throws XunjiaException
	 */
	public List<HyperlinkAdvInfo> getHyperlinkAdvInfoByPositon(final String position)
			throws XunjiaException {
		return (List<HyperlinkAdvInfo>) this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query q = session.createQuery("FROM HyperlinkAdvInfo AS h WHERE h.position like ? AND h.status=3");
				q.setString(0, position+"%");
				return q.list();
			}});
	}

}
